package customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONArray;

import customer.zspt.cn.customer.R;

/**
 * Created by Administrator on 2018/8/16 0016.
 */

public class DateIntervalStartView extends BaseView implements View.OnClickListener{
    private  LinearLayout start_ll;
    private  LinearLayout end_ll;
    private  TextView date_Start;
    private  TextView date_Sttitle;
    private  TextView date_Edtitle;
    private  TextView date_End;
    private DatePicker datePicker;
     //开始区间
    private String startTime;
    //结束区间
    private String endTime;
    //传入标题，是否是日期区间，是则显示一个日期
    public DateIntervalStartView(final Context context,
                                 boolean isInterval, String titleValues, JSONArray defaultValues, String dateValue) {
        super(context,titleValues);
        View.inflate(context,
                R.layout.chat_order_content_dateinterval_strart,this);
        start_ll = (LinearLayout) findViewById(R.id.start_ll);
        end_ll = (LinearLayout) findViewById(R.id.end_ll);
        start_ll.setOnClickListener(this);
        end_ll.setOnClickListener(this);
        //开始区间
        date_Sttitle = (TextView) findViewById(R.id.date_Sttitle);
        date_Sttitle.setText("日期区间[开始]");
        date_Start = (TextView)findViewById(R.id.date_Start);

        if(isInterval){
            end_ll.setVisibility(GONE);
            date_Sttitle.setText(titleValues);
            date_Start.setText(dateValue);
        }else{
            try{
                date_Start.setText(defaultValues.get(0).toString());
                //结束区间
                date_Edtitle = (TextView) findViewById(R.id.date_Edtitle);
                date_Edtitle.setText("日期区间[结束]");
                date_End = (TextView)findViewById(R.id.date_End);
                date_End.setText(defaultValues.get(1).toString());
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
    /**弹出日期选择框*/
    private void showDialog(Context context,String resultStr,final TextView contentTv) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        View view = View.inflate(context,
                R.layout.chat_order_content_dateinterval_datepicker,null);
        datePicker = view.findViewById(R.id.date_picker);
        dialog.setView(view);
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int month = datePicker.getMonth();
                int year = datePicker.getYear();
                int dayOfMonth = datePicker.getDayOfMonth();
                contentTv.setText(year+"-"+month+"-"+dayOfMonth);
            }
        });
        dialog.setNegativeButton("取消",null);
        dialog.show();
    }
    @Override
    public String result() {
        return date_Start.getText().toString();
    }
    @Override
    public void onClick(View v) {
         switch (v.getId()){
             case R.id.end_ll:
                 showDialog(getContext(),endTime,date_End);
                 break;
             case R.id.start_ll:
                 showDialog(getContext(),startTime,date_Start);
                 break;
         }
    }
}
